package triangle;

/**
 * This file is part of Rodney Ryan's submission of
 * his solution to the "Triangle Puzzle".
 *
 * rodney@rodneyryan.com
 */
public class TriangleArray implements Triangle {

    private long[][] array;
    private int rowIndex;

    public TriangleArray(long[][] array) {
        super();
        this.array = array;
        this.rowIndex = array.length - 1;
    }

    @Override
    public int getNumberOfRows() {
        return array.length;
    }

    @Override
    public int loadRow(long[] buffer) {
        if (rowIndex == -1) {
            return 0;
        } else {
            int length = array[rowIndex].length;
            System.arraycopy(array[rowIndex], 0, buffer, 0, length);
            rowIndex--;
            return length;
        }
    }

    public void dispose() {
        array = null;
    }
}